﻿/*
 * Created by SharpDevelop.
 * User: Mercury
 * Date: 2017/3/30
 * Time: 19:10
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Linq;
using ImageBrowser.Model;
using Win32;

namespace ImageBrowser
{
	/// <summary>
	/// Interaction logic for MainFrame.xaml
	/// </summary>
	public partial class MainFrame : Window
	{
        CurWallPaperConfig curConfig = new CurWallPaperConfig();

        // 图片集
        ObservableCollection<ImageInformation> imageCollection = new ObservableCollection<ImageInformation>();

        // 文件夹集
        ObservableCollection<FolderInformation> folderCollection = new ObservableCollection<FolderInformation>();

        // 文件扩展名集
        HashSet<string> extFileName = new HashSet<string>();

        // 放大展示的图片
        ShowImageInformation showImageInfor = new ShowImageInformation();

        public MainFrame()
		{
			InitializeComponent();

            InitParamter();

            BindingParameter();

            // test
            //ImageInformation image = new ImageInformation();
            //image.ImagePath = "d:\\bg.jpg";
            //imageCollection.Add(image);
            
            // test
        }

        private void InitParamter()
        {
            // 取得当前墙纸的路径
            StringBuilder Wallpaper = new StringBuilder(300);
            Win32Wrapper.SystemParametersInfo(Win32Wrapper.SPI_GETDESKWALLPAPER, 300, Wallpaper, 0);

            curConfig.CurWallPaperPath = Wallpaper.ToString();

            extFileName.Add(".jpg");
            extFileName.Add(".bmp");
            extFileName.Add(".png");
            extFileName.Add(".jpeg");

            ImageFolderPathListBox.ItemsSource = folderCollection;
            ImageListBox.ItemsSource = imageCollection;
        }

        private void BindingParameter()
        {
            ImageDisplay.SetBinding(Image.SourceProperty, new Binding("ImagePath") { Source = showImageInfor });
            BtnRestore.SetBinding(Button.IsEnabledProperty, new Binding("Restore") { Source = curConfig });
        }

        private void MenuItem_SetDeskBackground(object sender, RoutedEventArgs e)
        {
            StringBuilder imagePath = new StringBuilder((ImageListBox.SelectedItem as ImageInformation).ImagePath);
            Win32Wrapper.SystemParametersInfo(Win32Wrapper.SPI_SETDESKWALLPAPER, 0, imagePath, 1);
            ShowMsg.Text = "设置墙纸完成。";
            curConfig.Restore = true;
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            StringBuilder imagePath = new StringBuilder(curConfig.CurWallPaperPath);
            Win32Wrapper.SystemParametersInfo(Win32Wrapper.SPI_SETDESKWALLPAPER, 0, imagePath, 1);
        }

        private void Btn_ClickedOpenFolder(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // 如果已经包含这个路径就跳过
                if (folderCollection.Any(item => item.FolderPath == dialog.SelectedPath) == true)
                    return;
                FolderInformation folderInfo = new FolderInformation();
                folderInfo.FolderPath = dialog.SelectedPath;
                folderInfo.Selected = true;
                folderCollection.Add(folderInfo);
                ThreadPool.QueueUserWorkItem(SearchImage, dialog.SelectedPath);
            }
        }

        private void SearchImage(object parameter)
        {
            string folderPath = parameter as string;
            string[] files = Directory.GetFiles(folderPath);

            foreach (string file in files)
            {
                if (extFileName.Contains(Path.GetExtension(file).ToLower()) == true)
                {
                    // 如果已经包含这个文件了，就跳过
                    if (imageCollection.Any(p => p.ImagePath == file) == true)
                        continue;

                    ImageInformation image = new ImageInformation();
                    image.ImagePath = file;
                    image.ImageName = Path.GetFileName(file);
                    this.Dispatcher.BeginInvoke((Action)(()=> {
                        imageCollection.Add(image);
                    }));
                }
            }
        }

        private void Btn_ClickRestore(object sender, RoutedEventArgs e)
        {
            StringBuilder imagePath = new StringBuilder(curConfig.CurWallPaperPath);
            Win32Wrapper.SystemParametersInfo(Win32Wrapper.SPI_SETDESKWALLPAPER, 0, imagePath, 1);
            ShowMsg.Text = "恢复墙纸";
            curConfig.Restore = false;
        }

        private void ImageOverview_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            // 只处理双击事件
            if (e.ClickCount != 2)
                return;

            ImageInformation image = ImageListBox.SelectedItem as ImageInformation;
            if (image != null)
            {
                showImageInfor.ImagePath = image.ImagePath;
                ImageShowGrid.Visibility = Visibility.Visible;
                ImageListBox.Visibility = Visibility.Hidden;
            }
        }

        private void ImageDisplay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ClickCount != 2)
                return;

            ImageShowGrid.Visibility = Visibility.Hidden;
            ImageListBox.Visibility = Visibility.Visible;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (FolderGrid.Visibility == Visibility.Visible)
                FolderGrid.Visibility = Visibility.Collapsed;
            else if (FolderGrid.Visibility == Visibility.Collapsed)
                FolderGrid.Visibility = Visibility.Visible;

        }
    }
}